package com.jeff.tianti.controller;

import com.jeff.tianti.cms.dto.AwardRecordQueryDTO;
import com.jeff.tianti.cms.dto.CertificateQueryDTO;
import com.jeff.tianti.cms.dto.ColumnInfoQueryDTO;
import com.jeff.tianti.cms.entity.AwardRecord;
import com.jeff.tianti.cms.entity.Certificate;
import com.jeff.tianti.cms.entity.ColumnInfo;
import com.jeff.tianti.cms.service.AwardRecordService;
import com.jeff.tianti.cms.service.CertificateService;
import com.jeff.tianti.cms.service.ColumnInfoService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.common.util.ExcelUtils;
import com.jeff.tianti.org.entity.Role;
import com.jeff.tianti.org.entity.User;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/awardRecord")
public class AwardRecordController {
    @Autowired
    ColumnInfoService columnInfoService;
    @Autowired
    AwardRecordService awardRecordService;

    /**
     * 获取 友联列表
     *
     * @return
     */
    @RequestMapping("/list")
    public String index(HttpServletRequest request, Model model) {
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        String startAwardTime = request.getParameter("startAwardTime");
        String endAwardTime = request.getParameter("endAwardTime");
        String columnId = request.getParameter("columnId");
        String name = request.getParameter("name");

        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setCode(Constants.CODE_ZYJX);
        columnInfoQueryDTO.setIsCodeLike(false);
        List<ColumnInfo> columnInfos = columnInfoService.queryColumnInfoList(columnInfoQueryDTO);
        List<ColumnInfo> awardRecordCateList = null;

        if (!CollectionUtils.isEmpty(columnInfos)) {
            ColumnInfoQueryDTO parentColumnInfoQueryDTO = new ColumnInfoQueryDTO();
            parentColumnInfoQueryDTO.setIsRootColumnLike(false);
            parentColumnInfoQueryDTO.setRootColumnId(columnInfos.get(0).getId());
            awardRecordCateList = columnInfoService.queryColumnInfoList(parentColumnInfoQueryDTO);
        }

        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        AwardRecordQueryDTO awardRecordQueryDTO = new AwardRecordQueryDTO();
        awardRecordQueryDTO.setName(name);
        awardRecordQueryDTO.setColumnId(columnId);
        awardRecordQueryDTO.setCurrentPage(currentPage);
        awardRecordQueryDTO.setPageSize(pageSize);
        awardRecordQueryDTO.setStartAwardTime(startAwardTime);
        awardRecordQueryDTO.setEndAwardTime(endAwardTime);

        PageModel<AwardRecord> page = awardRecordService.queryCertificatePage(awardRecordQueryDTO);
        List<AwardRecord> list = page.getList();
        for (int i = 0; i < list.size(); i++) {
            AwardRecord awardRecord = list.get(i);
            String columnIdStr = awardRecord.getColumnId();
            ColumnInfo columnInfo =null;
            if (StringUtils.isNotBlank(columnIdStr)){
                columnInfo= columnInfoService.find(columnIdStr);
            }

            if (columnInfo != null) {
                awardRecord.setPrizeName(columnInfo.getName());
            }
        }
        model.addAttribute("page", page);
        model.addAttribute("awardRecordQueryDTO", awardRecordQueryDTO);
        model.addAttribute("awardRecordCateList", awardRecordCateList);
        model.addAttribute(Constants.MENU_NAME, Constants.MENU_AWARD_RECORD);
        return "/cms/AwardRecord/award_record_list";
    }

    /**
     * 跳转到友情编辑页
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(HttpServletRequest request, Model model) {

        String id = request.getParameter("id");
        AwardRecord awardRecord = awardRecordService.find(id);

        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setCode(Constants.CODE_ZYJX);
        columnInfoQueryDTO.setIsCodeLike(false);
        List<ColumnInfo> columnInfos = columnInfoService.queryColumnInfoList(columnInfoQueryDTO);
        List<ColumnInfo> awardRecordCateList = null;

        if (!CollectionUtils.isEmpty(columnInfos)) {
            ColumnInfoQueryDTO parentColumnInfoQueryDTO = new ColumnInfoQueryDTO();
            parentColumnInfoQueryDTO.setIsRootColumnLike(false);
            parentColumnInfoQueryDTO.setRootColumnId(columnInfos.get(0).getId());
            awardRecordCateList = columnInfoService.queryColumnInfoList(parentColumnInfoQueryDTO);
        }


        model.addAttribute("awardRecordCateList", awardRecordCateList);
        model.addAttribute("awardRecord", awardRecord);

        return "/cms/AwardRecord/dialog/award_record_edit";
    }


    @RequestMapping("/batchUpload")
    public String batchUpload(HttpServletRequest request, Model model) {

        String id = request.getParameter("id");
        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setCode(Constants.CODE_ZYJX);
        columnInfoQueryDTO.setIsCodeLike(false);
        List<ColumnInfo> columnInfos = columnInfoService.queryColumnInfoList(columnInfoQueryDTO);
        List<ColumnInfo> awardRecordCateList = null;

        if (!CollectionUtils.isEmpty(columnInfos)) {
            ColumnInfoQueryDTO parentColumnInfoQueryDTO = new ColumnInfoQueryDTO();
            parentColumnInfoQueryDTO.setIsRootColumnLike(false);
            parentColumnInfoQueryDTO.setRootColumnId(columnInfos.get(0).getId());
            awardRecordCateList = columnInfoService.queryColumnInfoList(parentColumnInfoQueryDTO);
        }


        model.addAttribute("awardRecordCateList", awardRecordCateList);


        return "/cms/AwardRecord/dialog/award_record_batch_upload";
    }

    /**
     * 栏目保存操作
     *
     * @param request
     * @return
     */
    @RequestMapping("/ajax/save")
    @ResponseBody
    public AjaxResult doFriendShipAjaxSave(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String name = request.getParameter("name");
            String company = request.getParameter("company");
            String columnId = request.getParameter("columnId");
            String level = request.getParameter("level");
            String awardTimeStr = request.getParameter("awardTime");

            Date awardTime =null;
            if (StringUtils.isNotBlank(awardTimeStr)){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                awardTime = sdf.parse(awardTimeStr);
            }

            AwardRecord awardRecord = new AwardRecord();
            awardRecord.setId(id);
            awardRecord.setName(name);
            awardRecord.setCompany(company);
            awardRecord.setLevel(level);
            awardRecord.setAwardTime(awardTime);
            awardRecord.setColumnId(columnId);

            if (StringUtils.isNotBlank(id)) {
                awardRecordService.updateSelective(awardRecord);
            } else {
                awardRecordService.save(awardRecord);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return ajaxResult;
    }

    /**
     * 栏目保存操作
     *
     * @param request
     * @return
     */
    @RequestMapping("/ajax/del")
    @ResponseBody
    public AjaxResult doFriendShipAjaxDel(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            awardRecordService.delete(id);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return ajaxResult;
    }
    /**
     * 导出用户数据
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/template/export")
    public String exportOrder(HttpServletRequest request, HttpServletResponse response){


        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        Map<String, String> headNameMap = new LinkedHashMap<String, String>();
        headNameMap.put("name", "作品名称");
        headNameMap.put("company", "获奖单位");
        headNameMap.put("level", "奖项等级(比如 金奖、银奖)");
        ExcelUtils.exportXlsx(response, "获奖记录导入模板", headNameMap, list);
        return null;
    }

}
